import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { resolve } from 'path';

@Module({
  imports: [],
  controllers: [AppController],
  // 简写
  // providers: [AppService],
  providers: [
    {
      provide: AppService,
      useClass: AppService
    },
    {
      provide: 'person',
      useValue: {
        name: 'aaa',
        age: 20
      }
    },
    // 动态注入
    {
      provide: 'person2',
      useFactory() {
        return {
          name: 'bbb',
          desc: 'cccc'
        }
      },
    },
    // 参数注入
    {
      provide: 'person3',
      useFactory(person: { name: string }, appService: AppService) {
        return {
          name: person.name,
          desc: appService.getHello()
        }
      }
    },
    // 异步注入
    {
      provide: 'person4',
      async useFactory() {
        await new Promise((resolve) => {
          setTimeout(resolve, 3000)
        });
        return {
          name: 'bbb',
          desc: 'cccc'
        }
      }
    },
    // 别名
    {
      provide: 'person2_Ex',
      useExisting: 'person2'
    }
  ]
})
export class AppModule { }
